﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tetris.Game.Common.TetrisCenter;

namespace Tetris.Game.ControlsBase
{
    public class CountDownControl : Control
    {
        private int count = 3;
        private System.Timers.Timer timer;
        public CountDownControl()
        {
            Font = new Font("隶书", 48, FontStyle.Bold);
            this.Size = new System.Drawing.Size(100, 100);
        }
        public Action CountDownEndCallback { get; set; }
        public void Start()
        {
            if (timer != null)
            {
                timer.Dispose();
                timer = null;
            }
            timer = new System.Timers.Timer();
            timer.Elapsed += Timer_Elapsed;
            this.ExecBeginInvoke(() =>
            {
                this.Visible = true;
            });
            count = 3;
            timer.Interval = 1000;
            timer.Start();
        }

        private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (--count >= 0)
            {
                this.ExecBeginInvoke(() =>
                {
                    Invalidate();
                });
                if(count==2)
                    SoundType.GameStart.PlaySound();
            }
            else
            {
                this.ExecBeginInvoke(() =>
                {
                    timer.Stop();
                    this.Visible = false;
                });
                CountDownEndCallback?.Invoke();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            count--;

            if (count >= 0)
            {
                this.ExecBeginInvoke(() =>
                {
                    Invalidate();
                });
            }
            else
            {
                this.ExecBeginInvoke(() =>
                {
                    timer.Stop();
                    this.Visible = false;
                });
                CountDownEndCallback?.Invoke();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 创建一个与控件大小相同的位图
            Bitmap buffer = new Bitmap(Width, Height);

            using (Graphics g = Graphics.FromImage(buffer))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.Clear(Color.Transparent);
                using Brush brush = new SolidBrush(ForeColor);
                int centerX = Width >> 1;
                int centerY = Height >> 1;

                g.DrawString(count.ToString(), Font, brush, centerX, centerY, new StringFormat()
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                });
            }

            // 将位图绘制到控件上
            e.Graphics.DrawImage(buffer, 0, 0);
            buffer.Dispose();
        }
    }
}
